package com.sf.proxy.chain.handler;

import com.sf.proxy.chain.ChainContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("blackNameUrlHandler")
@Slf4j
public class BlackNameUrlHandler implements Handler {


    private List<String> list;

    public BlackNameUrlHandler(){
        String[] blackNameList = { ".jpeg",  ".jpg",  ".ico",  ".gif",  ".png",
                ".js",  ".css",  ".otf",  ".eot",  ".svg",
                ".ttf", ".woff", ".JPEG", ".JPG",  ".ICO",
                ".GIF", ".PNG",  ".JS", 	 ".CSS",  ".OTF",
                ".EOT", ".SVG",  ".TTF",  ".WOFF", ".map",
                ".MAP"};
        list = Arrays.asList(blackNameList);
        Collections.unmodifiableList(list);
    }



    @Override
    public void handle(ChainContext context) {
        if (context.getRequestUrl().contains(".jsp")||context.getRequestUrl().contains(".JSP")){
            log.info("the url is jsp ..."+context.getRequestUrl());
            return;
        }
        log.info("start process the black name list ...");
        for (String blackName:list){
            if (context.getRequestUrl().contains(blackName)){
                log.warn("the url is in the black name list ..."+context.getRequestUrl());
                throw new RuntimeException("the url is in the blackName list ...");
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }
}
